package com.mf.ergate.web.controller.sys;

import com.mf.ergate.api.sys.model.dto.DistrictInfoCreateDto;
import com.mf.ergate.api.sys.model.dto.DistrictInfoDeleteDto;
import com.mf.ergate.api.sys.model.dto.DistrictInfoQueryDto;
import com.mf.ergate.api.sys.model.dto.DistrictInfoUpdateDto;
import com.mf.ergate.api.sys.model.vo.DistrictInfoVo;
import com.mf.ergate.web.service.sys.DistrictInfoService;
import com.mf.framework.common.model.Page;
import com.mf.ergate.web.common.utils.MfParamUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 行政区信息Controller
 * User: zhaoming
 * DateTime: 2019-03-21
 * To change this template use File | Settings | File Templates.
 */

@Secured({"PUBLIC"})
@Controller
@RequestMapping("/web/districtInfo")
public class DistrictInfoController{

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private DistrictInfoService districtInfoService;

    /**
     * 首页
     * @return 模型视图
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView("sys/district/district_info_index");
        return modelAndView;
    }

    /**
     * 新增页面
     * @return 模型视图
     */
    @RequestMapping(value = "/addPage", method = RequestMethod.GET)
    public ModelAndView addPage() {
        ModelAndView modelAndView = new ModelAndView("sys/district/district_info_add");
        return modelAndView;
    }

    /**
     * 维护页面
     * @param uniqueNo 唯一编号
     * @return 模型视图
     */
    @RequestMapping(value = "/editPage", method = RequestMethod.GET)
    public ModelAndView editPage(String uniqueNo) {
        ModelAndView modelAndView = new ModelAndView("sys/district/district_info_edit");
        modelAndView.addObject("uniqueNo", uniqueNo);
        return modelAndView;
    }

    /**
     * 新增
     * @param districtInfoCreateDto 行政区信息新增
     * @return 行政区信息
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public DistrictInfoVo insert(DistrictInfoCreateDto districtInfoCreateDto) {
        MfParamUtils.buildInsertEntity(districtInfoCreateDto);
        return districtInfoService.insert(districtInfoCreateDto);
    }

    /**
     * 更新
     * @param districtInfoUpdateDto 行政区信息更新
     * @return 执行结果
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Boolean update(DistrictInfoUpdateDto districtInfoUpdateDto) {
        MfParamUtils.buildUpdateEntity(districtInfoUpdateDto);
        return districtInfoService.updateByUniqueNo(districtInfoUpdateDto);
    }

    /**
     * 根据主键删除(逻辑删除)
     * @param districtInfoDeleteDto 行政区信息删除
     * @return 执行结果
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public boolean delete(DistrictInfoDeleteDto districtInfoDeleteDto){
        MfParamUtils.buildUpdateEntity(districtInfoDeleteDto);
        return districtInfoService.deleteByUniqueNo(districtInfoDeleteDto);
    }

    /**
     * 根据业务主键查询
     * @param uniqueNo 唯一编号
     * @return 行政区信息
     */
    @RequestMapping(value = "/detail", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public DistrictInfoVo detail(String uniqueNo) {
        return districtInfoService.selectByUniqueNo(uniqueNo);
    }

    /**
     * 根据条件查询
     * @param districtInfoQueryDto 行政区信息查询
     * @return 行政区信息列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public List<DistrictInfoVo> list(DistrictInfoQueryDto districtInfoQueryDto) {
        return districtInfoService.selectByCondition(districtInfoQueryDto);
    }

    /**
     * 根据条件分页查询
     * @param districtInfoQueryDto 行政区信息查询
     * @param pageNum 当前页码
     * @param pageSize 每页显示条数
     * @param sortName 排序字段
     * @param sortOrder 排序方式
     * @return 行政区信息分页列表
     */
    @RequestMapping(value = "/page", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Page<DistrictInfoVo> page(DistrictInfoQueryDto districtInfoQueryDto,
                                    @RequestParam(value = "page") int pageNum,
                                    @RequestParam(value = "rows") int pageSize,
                                    @RequestParam(value = "sidx") String sortName,
                                    @RequestParam(value = "sord") String sortOrder) {

        Page page = MfParamUtils.buildPage(pageNum, pageSize, sortName, sortOrder);
        return districtInfoService.selectPage(page, districtInfoQueryDto);
    }

}